package com.example.itcompanyinterview;

import com.example.itcompanyinterview.object.*;
import com.example.itcompanyinterview.tools.ActivityAnimationSwitcher;
import com.example.itcompanyinterview.tools.MenuHelper;
import com.example.itcompanyinterview.tools.TitleBarGenerator;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.method.ScrollingMovementMethod;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class QuestionAnswerActivity extends Activity {

	private InterviewQuestion question;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.activity_question_answer);
		this.initActivityTitleBar();
		this.initViews();
		this.setQuestion();
		this.getAnswerFromParent();
	}

	private void initActivityTitleBar() {
		TitleBarGenerator titleBar = new TitleBarGenerator(this);
		titleBar.setReturnButtonView(View.GONE);
		titleBar.setTitleText("解答").setActionText("回到题目");
		titleBar.showTitleBar();
		this.setButtonListner();
	}
	
	private void setButtonListner() {
		final Button titleActionButton = (Button)this.findViewById(R.id.title_action_button);
        titleActionButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}

		});
	}
	
	private void initViews() {
		TextView tv = (TextView)this.findViewById(R.id.answerDescription);
		tv.setMovementMethod(new ScrollingMovementMethod());
	}

	private void setQuestion() {
		Intent intent = this.getIntent();
		Bundle bundle = intent.getExtras();
		this.question = (InterviewQuestion) bundle.getSerializable("question");
	}
	
	private void getAnswerFromParent(){
		TextView titleView = (TextView) this.findViewById(R.id.answerTitle);
		TextView answerView = (TextView) this
				.findViewById(R.id.answerDescription);
		titleView.setText(this.question.getTitle());
		answerView.setText(this.question.getAnswer());
	}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	MenuHelper menuHelper = new MenuHelper(this, item);
    	menuHelper.processMenu();
    	return super.onOptionsItemSelected(item);
    }

	@Override
	protected void onResume() {
		ActivityAnimationSwitcher.animationIn(findViewById(R.id.container), getWindowManager());
		super.onResume();
	}

	@Override
	public void finish() {
		ActivityAnimationSwitcher.animationOut(findViewById(R.id.container), getWindowManager(), 
				new ActivityAnimationSwitcher.AnimationFinishedListener() {
			@Override
			public void onAnimationFinished() {
				QuestionAnswerActivity.super.finish();
				// disable default animation
				overridePendingTransition(0, 0);
			}
		});
	}
	
	

}
